const http=require('http');
const fs=require('fs');

var server=http.createServer(function (req, res){
    res.write('<head><meta charset="utf-8"/></head>');
    var file_name='./www'+req.url;
    //readFile(文件名，编码格式，回调函数)
    fs.readFile(file_name,"utf-8", function (err, data){
        if(err){
            res.write('读取失败');
        }else{
            res.write(data);//data.toString()用途同"utf-8"
        }
        res.end();
    });
});

//writeFile(文件名，内容，回调函数)
fs.writeFile("./www/abc.txt","I love you我爱你",function (err) {
    if(err){
        console.log('写入失败');
    }else{
        console.log('写入成功');
    }
});

server.listen(8888);



